package com.yc.booktable.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yc.booktable.entity.AdminUser;
import com.yc.booktable.mapper.AdminUserMapper;
import com.yc.booktable.service.IAdminUserService;
import com.yc.booktable.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.security.URIParameter;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zbh
 * @since 2022-06-06
 */
@Controller
@RequestMapping("/adminUser")
public class AdminUserController {

    @Autowired
    private IAdminUserService userService;
    @Resource
    private AdminUserMapper mapper;

    @PostMapping("login")
    @ResponseBody
    public Result login(String uaccount, String upassword, HttpSession session){
        QueryWrapper<AdminUser> wrapper = new QueryWrapper<>();
        wrapper.eq("account",uaccount).eq("password",upassword);
        AdminUser user = userService.getOne(wrapper);
        if(user==null){
            return new Result(0, "用户名或密码错误", null);
        }else {
            session.setAttribute("loginedUser", user);
            System.out.println(session);
            return new Result(1, "登录成功", user.getState());
        }
    }
    @GetMapping("logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "index";
    }

    @PostMapping("register")
    @ResponseBody
    public Result register(String uname,String uacc,String upwd,String uemail,String uphone,String uaddress){

        Integer acc = (uacc==null||uacc.length()==0)?0:Integer.parseInt(uacc);
        Integer phone = (uphone==null||uphone.length()==0)?0:Integer.parseInt(uphone);

        AdminUser adminUser = new AdminUser(null, uname, acc, upwd, phone, uemail, uaddress, 1);
        int insert = mapper.insert(adminUser);
        if(insert==0){
            return new Result(0,"注册失败",null);
        }else{
            return new Result(1,"注册成功",null);
        }
    }

    @PostMapping("updateUser")
    @ResponseBody
    public Result updateUser(String id,String account,String email,String phone,String name,String password,String address , HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        long lid = Long.parseLong(id);
        Integer acc = Integer.parseInt(account);
        int p = Integer.parseInt(phone);
        AdminUser user = new AdminUser(lid, name, acc, password, p, email, address, 1);
        try {
            boolean update = userService.updateById(user);
            if(update){
                return new Result(1,"修改成功",user);
            }else{
                return new Result(0,"修改失败",null);
            }
        }catch (Exception e){
            return new Result(0,"email或者电话重复",null);
        }

    }

}

